﻿<UserControl x:Class="BackgroundProcessing.RunningActionsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:cal="http://www.caliburnproject.org"
             xmlns:conv="clr-namespace:BackgroundProcessing.Converters"
             Visibility="{Binding Path=RunningActions.Count, Converter={x:Static conv:HiddenWhenLowerConverter.Instance}, ConverterParameter=1}">

	<UserControl.Resources>
		<DataTemplate x:Key="runningActionTemplate">
			<Grid>
				<Grid.ColumnDefinitions>
					<ColumnDefinition Width="auto"
					                  SharedSizeGroup="titles" />
					<ColumnDefinition Width="200" />
					<ColumnDefinition Width="auto" />
				</Grid.ColumnDefinitions>

				<Label Content="{Binding Path=Title}"
				       Grid.Column="0" />
				<ProgressBar Margin="10 0 0 0"
				             Value="{Binding Path=CurrentPercentage, Mode=OneWay}"
				             IsIndeterminate="{Binding Path=IsIndeterminate}"
				             Maximum="1"
				             LargeChange="0.1"
				             SmallChange="0.01"
				             Grid.Column="1" />

				<Button cal:Message.Attach="Cancel"
				        cal:Action.TargetWithoutContext="{Binding}"
				        Visibility="{Binding Path=IsCancellable, Converter={StaticResource booleanToVisibility}}"
				        Grid.Column="2">
					Cancel
				</Button>
			</Grid>
		</DataTemplate>

	</UserControl.Resources>

	<Grid>
		<!--one running action only-->
		<ContentControl Visibility="{Binding Path=RunningActions.Count, Converter={x:Static conv:HiddenWhenHigherConverter.Instance}, ConverterParameter=1}"
		                Content="{Binding RunningActions[0]}"
		                ContentTemplate="{StaticResource runningActionTemplate}" />

		<!--multiple running actions-->
		<StackPanel Orientation="Horizontal"
		            Visibility="{Binding Path=RunningActions.Count, Converter={x:Static conv:HiddenWhenLowerConverter.Instance}, ConverterParameter=2}">

			<Label>
				<TextBlock>
					<TextBlock Text="{Binding Path=RunningActions.Count}" />
					<TextBlock Text=" running actions" />
				</TextBlock>
			</Label>
			<Button Name="btnDetails"
			        cal:Message.Attach="[Event Click] = [Action ShowDetails()]">
				Details
			</Button>

			<Popup Name="detailsPopup"
			       PlacementTarget="{Binding ElementName=btnDetails}"
			       Placement="Relative"
			       StaysOpen="False"
			       AllowsTransparency="True"
			       VerticalOffset="{Binding ElementName=btnDetails, Path=ActualHeight}">
				<Border BorderBrush="Black"
				        BorderThickness="1"
				        CornerRadius="5"
				        Background="White"
				        Padding="3"
				        Margin="5">
					<Border.Effect>
						<DropShadowEffect />
					</Border.Effect>

					<ItemsControl ItemsSource="{Binding RunningActions}"
					              Grid.IsSharedSizeScope="True"
					              ItemTemplate="{StaticResource runningActionTemplate}" />
				</Border>
			</Popup>

			<Button cal:Message.Attach="CancelAll">Cancel all</Button>
		</StackPanel>
	</Grid>
</UserControl>